/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.pod.hoteishilton.gui;

import edu.pod.hoteishilton.facade.IFacade;
import edu.pod.hoteishilton.model.*;
import java.awt.Button;
import java.awt.event.KeyEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Tigaz
 */
public class ServerMain extends javax.swing.JFrame {

    /**
     * Creates new form Main
     */
    public ServerMain() {
        initComponents();

        try {
            facade = (IFacade) Naming.lookup("//localhost:12346/HoteisHiltonServer");


        } catch (NotBoundException ex) {
            Logger.getLogger(ServerMain.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ServerMain.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(ServerMain.class.getName()).log(Level.SEVERE, null, ex);
        }

        initDadosComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuHoteisjPopupMenu = new javax.swing.JPopupMenu();
        gerenciasQuartosjMenuItem = new javax.swing.JMenuItem();
        removerHoteljMenuItem = new javax.swing.JMenuItem();
        quartosjDialog = new javax.swing.JDialog();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jSplitPane2 = new javax.swing.JSplitPane();
        jPanel7 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        numQuartosjTextField = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        valorQuartojTextField = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        adicionarQuartosjButton = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        quartosjTable = new javax.swing.JTable();
        jPanel9 = new javax.swing.JPanel();
        apelidoHotelTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        addHotelButton = new javax.swing.JButton();
        gerenteHotelTextField = new javax.swing.JTextField();
        endHotelTextField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        telHotelTextField = new javax.swing.JTextField();
        jPanel10 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        hoteisTable = new javax.swing.JTable();
        filtroHoteisjTextField = new javax.swing.JTextField();
        filtrarHoteisJButton = new javax.swing.JButton();
        limparHoteisjButton = new javax.swing.JButton();

        menuHoteisjPopupMenu.setComponentPopupMenu(menuHoteisjPopupMenu);

        gerenciasQuartosjMenuItem.setText("Gerenciar Quarto(s)");
        gerenciasQuartosjMenuItem.setComponentPopupMenu(menuHoteisjPopupMenu);
        gerenciasQuartosjMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gerenciasQuartosjMenuItemActionPerformed(evt);
            }
        });
        menuHoteisjPopupMenu.add(gerenciasQuartosjMenuItem);

        removerHoteljMenuItem.setText("Remover Hotel");
        removerHoteljMenuItem.setComponentPopupMenu(menuHoteisjPopupMenu);
        menuHoteisjPopupMenu.add(removerHoteljMenuItem);

        quartosjDialog.setTitle("Quartos");

        javax.swing.GroupLayout quartosjDialogLayout = new javax.swing.GroupLayout(quartosjDialog.getContentPane());
        quartosjDialog.getContentPane().setLayout(quartosjDialogLayout);
        quartosjDialogLayout.setHorizontalGroup(
            quartosjDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 299, Short.MAX_VALUE)
        );
        quartosjDialogLayout.setVerticalGroup(
            quartosjDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 459, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Hoteis Hilton");
        setResizable(false);
        setType(java.awt.Window.Type.POPUP);

        jSplitPane2.setDividerLocation(350);

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Dados Hotel"));
        jPanel8.setPreferredSize(new java.awt.Dimension(329, 400));

        jLabel16.setText("Numero de quartos");

        valorQuartojTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                valorDiariaJtextFieldKeyReleased(evt);
            }
        });

        jLabel15.setText("Valor da diaria R$");

        adicionarQuartosjButton.setText("Adicionar Quarto(s)");
        adicionarQuartosjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adicionarQuartosjButtonActionPerformed(evt);
            }
        });

        quartosjTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Numero", "Diaria"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane4.setViewportView(quartosjTable);

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel16)
                            .addComponent(jLabel15))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(numQuartosjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(valorQuartojTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(122, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(adicionarQuartosjButton)
                        .addGap(91, 91, 91))))
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(numQuartosjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(valorQuartojTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(adicionarQuartosjButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(232, 232, 232))
        );

        jPanel9.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        apelidoHotelTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                pesquisarHotelFocusLost(evt);
            }
        });
        apelidoHotelTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                nomeHotelJtextFieldKeyReleased(evt);
            }
        });

        jLabel6.setText("Endereço");

        addHotelButton.setText("Cadastrar/Editar");
        addHotelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addHotelButtonActionPerformed(evt);
            }
        });

        gerenteHotelTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                gerenteHotelJtextFieldKeyReleased(evt);
            }
        });

        endHotelTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                endHotelJtextFieldKeyReleased(evt);
            }
        });

        jLabel7.setText("Gerente");

        jLabel8.setText("Nome*");

        jLabel13.setText("Telefone");

        telHotelTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                telHotelJtextFieldKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGap(119, 119, 119)
                        .addComponent(addHotelButton))
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel6)
                            .addComponent(jLabel13)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(telHotelTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(gerenteHotelTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(endHotelTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(apelidoHotelTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(69, Short.MAX_VALUE))
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                .addContainerGap(32, Short.MAX_VALUE)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(apelidoHotelTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(endHotelTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(gerenteHotelTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(telHotelTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addGap(18, 18, 18)
                .addComponent(addHotelButton)
                .addGap(19, 19, 19))
        );

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jSplitPane2.setLeftComponent(jPanel7);

        hoteisTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Endereço", "Gerente", "Telefone"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        hoteisTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                mostrarMenuHoteisMouseReleased(evt);
            }
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                hoteisTableClicked(evt);
            }
        });
        jScrollPane2.setViewportView(hoteisTable);

        filtroHoteisjTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                filtroHosteisButtonPressed(evt);
            }
        });

        filtrarHoteisJButton.setText("Pesquisar");
        filtrarHoteisJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filtrarHoteisJButtonActionPerformed(evt);
            }
        });

        limparHoteisjButton.setText("Limpar");
        limparHoteisjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limparHoteisjButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel10Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel10Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(filtroHoteisjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(filtrarHoteisJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(limparHoteisjButton, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(13, 13, 13))))
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(filtroHoteisjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(filtrarHoteisJButton)
                    .addComponent(limparHoteisjButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 529, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        jSplitPane2.setRightComponent(jPanel10);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane2)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane2)
        );

        jTabbedPane1.addTab("Hoteis", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 886, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 628, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void gerenciasQuartosjMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gerenciasQuartosjMenuItemActionPerformed
        quartosjDialog.pack();
        quartosjDialog.setVisible(true);
    }//GEN-LAST:event_gerenciasQuartosjMenuItemActionPerformed

    private void limparHoteisjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limparHoteisjButtonActionPerformed
        apelidoHotelTextField.setText(null);
        endHotelTextField.setText(null);
        gerenteHotelTextField.setText(null);
        telHotelTextField.setText(null);

        filtroHoteisjTextField.setText(null);

        sorterHoteisTable.setRowFilter(RowFilter.regexFilter(""));

        apelidoHotelTextField.setEditable(true);
        ((DefaultTableModel) quartosjTable.getModel()).setRowCount(0);
        numQuartosjTextField.setText(null);
        valorQuartojTextField.setText(null);
        hotel = null;
        quartos = null;
    }//GEN-LAST:event_limparHoteisjButtonActionPerformed

    private void filtrarHoteisJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filtrarHoteisJButtonActionPerformed
        sorterHoteisTable.setRowFilter(RowFilter.regexFilter(filtroHoteisjTextField.getText()));
    }//GEN-LAST:event_filtrarHoteisJButtonActionPerformed

    private void filtroHosteisButtonPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_filtroHosteisButtonPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            filtrarHoteisJButton.doClick();
        }
    }//GEN-LAST:event_filtroHosteisButtonPressed

    private void hoteisTableClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_hoteisTableClicked
        if (evt.getClickCount() == 2) {
            hotel = (Hotel) hoteis.get(hoteisTable.getValueAt(hoteisTable.getSelectedRow(), 0));

            preencherHotel(hotel);

        }
    }//GEN-LAST:event_hoteisTableClicked

    private void mostrarMenuHoteisMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mostrarMenuHoteisMouseReleased
        int r = hoteisTable.rowAtPoint(evt.getPoint());
        if (r >= 0 && r < hoteisTable.getRowCount()) {
            hoteisTable.setRowSelectionInterval(r, r);
        } else {
            hoteisTable.clearSelection();
        }

        int rowindex = hoteisTable.getSelectedRow();
        if (rowindex < 0) {
            return;
        }
        if (evt.isPopupTrigger() && evt.getComponent() instanceof JTable) {
            menuHoteisjPopupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_mostrarMenuHoteisMouseReleased

    private void addHotelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addHotelButtonActionPerformed
        try {
            if (apelidoHotelTextField.getText().trim().equals("")) {
                JOptionPane.showMessageDialog(null, "Campo nome Obrigatorio!", "Hotel", JOptionPane.WARNING_MESSAGE);
            } else {

                if (hoteis.containsKey(apelidoHotelTextField.getText())) {
                    hotel = (Hotel) hoteis.get(apelidoHotelTextField.getText());
                    hotel.setEndereco(endHotelTextField.getText());
                    hotel.setNomeGerente(gerenteHotelTextField.getText());
                    hotel.setTelefone(telHotelTextField.getText());
                } else {
                    hotel = new Hotel(apelidoHotelTextField.getText(), endHotelTextField.getText(), gerenteHotelTextField.getText(), telHotelTextField.getText());
                }

                facade.addHotel(hotel);
                hoteis.put(hotel.getApelido(), hotel);
                fillTableHoteis(hoteisTable, hoteis);
                JOptionPane.showMessageDialog(null, "Hotel Adicionado/Editado com Sucesso!", "Hotel", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (RemoteException ex) {
            Logger.getLogger(ServerMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_addHotelButtonActionPerformed

    private void pesquisarHotelFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_pesquisarHotelFocusLost
        if (hoteis.containsKey(apelidoHotelTextField.getText())) {
            hotel = (Hotel) hoteis.get(apelidoHotelTextField.getText());

            preencherHotel(hotel);

        }

    }//GEN-LAST:event_pesquisarHotelFocusLost

    private void adicionarQuartosjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionarQuartosjButtonActionPerformed
        if (hotel == null) {
            JOptionPane.showMessageDialog(null, "Favor selecionar o Hotel na Tabela", "Quartos", JOptionPane.WARNING_MESSAGE);
        } else {
            int ultimoQuarto = hotel.getUltimoQuarto() + 1;

            try {
                int numquartos = Integer.parseInt(numQuartosjTextField.getText());
                double preco = Double.parseDouble(valorQuartojTextField.getText());
                for (int i = ultimoQuarto; i < ultimoQuarto + numquartos; i++) {

                    Quarto q = new Quarto(i, preco);
                    q.setHotel(hotel);


                    hotel.addQuarto(q);
                    quartos = hotel.getQuartos();
                    facade.addHotel(hotel);
                    fillTableQuartos(quartosjTable, quartos);
                }
            } catch (RemoteException ex) {
                Logger.getLogger(ServerMain.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Numeros invalidos!", "Quartos", JOptionPane.WARNING_MESSAGE);
                return;
            }

        }
    }//GEN-LAST:event_adicionarQuartosjButtonActionPerformed

    private void nomeHotelJtextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nomeHotelJtextFieldKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            addHotelButton.doClick();
        }
    }//GEN-LAST:event_nomeHotelJtextFieldKeyReleased

    private void endHotelJtextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_endHotelJtextFieldKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            addHotelButton.doClick();
        }
    }//GEN-LAST:event_endHotelJtextFieldKeyReleased

    private void gerenteHotelJtextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_gerenteHotelJtextFieldKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            addHotelButton.doClick();
        }
    }//GEN-LAST:event_gerenteHotelJtextFieldKeyReleased

    private void valorDiariaJtextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_valorDiariaJtextFieldKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            adicionarQuartosjButton.doClick();
        }
    }//GEN-LAST:event_valorDiariaJtextFieldKeyReleased

    private void telHotelJtextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_telHotelJtextFieldKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            adicionarQuartosjButton.doClick();
        }
    }//GEN-LAST:event_telHotelJtextFieldKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ServerMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ServerMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ServerMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ServerMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ServerMain().setVisible(true);


            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addHotelButton;
    private javax.swing.JButton adicionarQuartosjButton;
    private javax.swing.JTextField apelidoHotelTextField;
    private javax.swing.JTextField endHotelTextField;
    private javax.swing.JButton filtrarHoteisJButton;
    private javax.swing.JTextField filtroHoteisjTextField;
    private javax.swing.JMenuItem gerenciasQuartosjMenuItem;
    private javax.swing.JTextField gerenteHotelTextField;
    private javax.swing.JTable hoteisTable;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton limparHoteisjButton;
    private javax.swing.JPopupMenu menuHoteisjPopupMenu;
    private javax.swing.JTextField numQuartosjTextField;
    private javax.swing.JDialog quartosjDialog;
    private javax.swing.JTable quartosjTable;
    private javax.swing.JMenuItem removerHoteljMenuItem;
    private javax.swing.JTextField telHotelTextField;
    private javax.swing.JTextField valorQuartojTextField;
    // End of variables declaration//GEN-END:variables
    private static IFacade facade;
    private HashMap hospedes;
    private HashMap hoteis;
    private ArrayList<Quarto> quartos;
    private HashMap reservas;
    private HashMap hospedagens;
    private Hospede hospede;
    private Hotel hotel;
    private Reserva reserva;
    private Hospedagem hospegagem;
    private TableRowSorter<TableModel> sorterHoteisTable;

    private void initDadosComponents() {
        try {
            hospedes = facade.getHospedes();
            hoteis = facade.getHoteis();


            sorterHoteisTable = new TableRowSorter<>(hoteisTable.getModel());
            hoteisTable.setRowSorter(sorterHoteisTable);
            fillTableHoteis(hoteisTable, hoteis);


        } catch (RemoteException ex) {
            Logger.getLogger(ServerMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void fillTableHospedes(JTable hospedesTable, HashMap hospedes) {
        DefaultTableModel tm = (DefaultTableModel) hospedesTable.getModel();
        String[] row = new String[4];

        tm.setRowCount(0);
        for (Iterator it = hospedes.values().iterator(); it.hasNext();) {
            Object h = it.next();
            row[0] = ((Hospede) h).getNome();
            row[1] = ((Hospede) h).getEndereco();
            row[2] = ((Hospede) h).getTelefone();
            row[3] = ((Hospede) h).getEmail();
            tm.addRow(row);
        }
        hospedesTable.setModel(tm);
    }

    private void fillTableHoteis(JTable hospedesTable, HashMap hoteis) {
        DefaultTableModel tm = (DefaultTableModel) hoteisTable.getModel();
        String[] row = new String[4];

        tm.setRowCount(0);
        for (Iterator it = hoteis.values().iterator(); it.hasNext();) {
            Object h = it.next();
            row[0] = ((Hotel) h).getApelido();
            row[1] = ((Hotel) h).getEndereco();
            row[2] = ((Hotel) h).getNomeGerente();
            row[3] = ((Hotel) h).getTelefone();
            tm.addRow(row);
        }
        hoteisTable.setModel(tm);
    }

    private void fillTableQuartos(JTable quartosTable, ArrayList<Quarto> quartos) {
        DefaultTableModel tm = (DefaultTableModel) quartosTable.getModel();
        String[] row = new String[2];

        tm.setRowCount(0);

        for (Quarto q : quartos) {
            row[0] = Integer.toString(q.getNumeroQuarto());
            row[1] = Double.toString(q.getPreco());
            tm.addRow(row);
        }
        quartosTable.setModel(tm);
    }

    private void preencherHotel(Hotel hotel) {


        apelidoHotelTextField.setText(hotel.getApelido());
        endHotelTextField.setText(hotel.getEndereco());
        gerenteHotelTextField.setText(hotel.getNomeGerente());
        telHotelTextField.setText(hotel.getTelefone());

        quartos = hotel.getQuartos();


        fillTableQuartos(quartosjTable, quartos);

        apelidoHotelTextField.setEditable(false);
        addHotelButton.setText("Editar");
    }
}
